package com.microservice.wechat.controller;

import com.microservice.wechat.service.WechatTokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 微信Token管理控制器
 */
@RestController
@RequestMapping("/wechat/token")
public class WechatTokenController {
    
    @Autowired
    private WechatTokenService tokenService;
    
    /**
     * 获取指定账号的Token
     */
    @GetMapping("/{appId}")
    public Map<String, Object> getToken(@PathVariable String appId) {
        Map<String, Object> result = new HashMap<>();
        try {
            String token = tokenService.getAccessToken(appId);
            result.put("success", true);
            result.put("appId", appId);
            result.put("token", token);
            result.put("message", "获取Token成功");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }
    
    /**
     * 强制刷新指定账号的Token
     */
    @PostMapping("/refresh/{appId}")
    public Map<String, Object> refreshToken(@PathVariable String appId) {
        Map<String, Object> result = new HashMap<>();
        try {
            String token = tokenService.forceRefreshToken(appId);
            result.put("success", true);
            result.put("appId", appId);
            result.put("token", token);
            result.put("message", "Token刷新成功");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }
    
    /**
     * 批量刷新所有账号的Token
     */
    @PostMapping("/refresh-all")
    public Map<String, Object> refreshAllTokens() {
        Map<String, Object> result = new HashMap<>();
        try {
            tokenService.refreshAllTokens();
            result.put("success", true);
            result.put("message", "批量刷新任务已启动");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }
    
    /**
     * 查看所有Token状态
     */
    @GetMapping("/status")
    public Map<String, Object> getStatus() {
        Map<String, Object> result = new HashMap<>();
        try {
            Map<String, Object> status = tokenService.getAllTokenStatus();
            result.put("success", true);
            result.put("data", status);
            result.put("message", "获取状态成功");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }
    
    /**
     * 随机获取一个账号的Token
     */
    @GetMapping("/random")
    public Map<String, Object> getRandomToken() {
        Map<String, Object> result = new HashMap<>();
        try {
            Map<String, String> tokenInfo = tokenService.getRandomAccessToken();
            result.put("success", true);
            result.put("data", tokenInfo);
            result.put("message", "随机获取Token成功");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }
    
    /**
     * 根据类型随机获取一个账号的Token
     */
    @GetMapping("/random/{configType}")
    public Map<String, Object> getRandomTokenByType(@PathVariable String configType) {
        Map<String, Object> result = new HashMap<>();
        try {
            Map<String, String> tokenInfo = tokenService.getRandomAccessTokenByType(configType);
            result.put("success", true);
            result.put("data", tokenInfo);
            result.put("message", "随机获取" + configType + "类型Token成功");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }
}
